<?php
/**
* @file $Id: Bottom.php 554 2007-06-04 05:16:09Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

error_reporting(1);
require_once("./Warehouse.php");

if($_REQUEST['modfunc']=='print')
{
	$_REQUEST = $_SESSION['_REQUEST_vars'];
	$_REQUEST['_FOCUS_PDF'] = true;
	if(strpos($_REQUEST['modname'],'?')!==false)
		$modname = substr($_REQUEST['modname'],0,strpos($_REQUEST['modname'],'?'));
	else
		$modname = $_REQUEST['modname'];

	if($_REQUEST['bottom_print_modname'])
	{
		$_REQUEST['modname'] = $_REQUEST['bottom_print_modname'];
		$modname = substr($_REQUEST['modname'],0,strpos($_REQUEST['modname'],'?'));
		if($_REQUEST['bottom_print_cpid'])
			$_REQUEST['course_period_id'] = $_REQUEST['bottom_print_cpid'];
	}
	ob_start();

	include('modules/'.$modname);

	if($_REQUEST['_template_id'])
		DrawTemplateHeaders('','footer');

//	$html = eregi_replace('</?CENTER>','',str_replace('assets/',"http://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')).'/assets/',ob_get_contents()));
//	$html = ereg_replace('((../)?uploaded-)?assets/',"http://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')).'/\\1assets/',ob_get_contents());
	$html = ereg_replace('=(["\'])?assets/',"\\1http://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')).'/assets/',ob_get_contents());
	
	//$html = eregi_replace('</?CENTER>','',ob_get_contents());
	ob_end_clean();
	if(!is_executable($htmldocPath))
		ErrorMessage(array(_("PDFs cannot be produced since HTMLDOC isn't working.  Please ensure that HTMLDOC is installed and that its path is configured in config.inc.php")), 'fatal');

	// get a temp filename, and then change its extension from .tmp to .html to make htmldoc happy.
	$temphtml=tempnam('','html');
	//$temphtml_tmp=substr($temphtml, 0, strrpos($temphtml, ".")).'html';
	//rename($temphtml_tmp, $temphtml); 
	
	$fp=@fopen($temphtml,"w+");
	if (!$fp) {
		die("Can't open $temphtml");
	}
	fputs($fp,'<HTML><BODY>'.$html.'</BODY></HTML>');
	@fclose($fp);

	header("Cache-Control: public");
	header("Pragma: ");
	header("Content-Type: application/pdf");
	header("Content-Disposition: inline; filename=\"".ProgramTitle().".pdf\"\n");

	$orientation = 'portrait';
	if($_REQUEST['expanded_view'] || $_SESSION['orientation'] == 'landscape')
	{	
		$orientation = 'landscape';
		unset($_SESSION['orientation']);
	}
	if($language!='en_US')
		$charset = '--charset iso-8859-1';
	passthru("$htmldocPath --webpage --quiet -t pdf12 --jpeg --no-links --$orientation $charset --footer t --header . --left 0.5in --top 0.5in \"$temphtml\"");
	@unlink($temphtml);
}
else
{
	if($language=='en_US')
		echo "<HTML>";
	else
		echo '<HTML lang="'.substr($language,0,2).'" xml:lang="'.substr($language,0,2).'">';
	echo "
	<HTML>
		<HEAD><TITLE>Focus School Software</TITLE>";
		if($language!='en_US')
			echo '<meta http-equiv="Content-Type" content="text/html; charset="UTF-8" /><meta http-equiv="Content-Language" content="'.strtolower(str_replace('_','-',$language)).'" />';

		echo "<SCRIPT>";
		if($_REQUEST['expanded']=='true')
			echo 'size = 200;';
		else
			echo 'size = 30;';
		echo "
		function expandFrame()
		{
			if(size==30)
			{
				parent.document.getElementById('mainframeset').rows=\"*,200\";
				size = 200;
			}
			else
			{
				parent.document.getElementById('mainframeset').rows=\"*,30\";
				size = 30;
			}
		}
		</SCRIPT>";

		if($_FOCUS['direction']=='RTL')
			echo '<link rel="stylesheet" type="text/css" media="all" href="assets/rtl.css" />';
		
		echo "<link rel=stylesheet type=text/css href=assets/themes/".Preferences('THEME')."/stylesheet.css>
		</HEAD>
		<BODY background=assets/themes/".Preferences('THEME')."/bg.jpg leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>";
	echo '<CENTER>';
	
	echo '<TABLE><TR>';
	if($_SESSION['List_PHP_SELF'] && User('PROFILE')!='parent' && User('PROFILE')!='student')
		echo '<TD width=24><A HREF='.$_SESSION['List_PHP_SELF'].'&bottom_back=true target=body><IMG SRC=assets/back.gif border=0 vspace=0></A></TD><TD valign=middle class=BottomButton><A HREF='.str_replace('&amp;bottom_back=true','',$_SESSION['List_PHP_SELF']).'&bottom_back=true target=body>'._('Back to Student List').'</A></TD>';
	if($_SESSION['Search_PHP_SELF'] && User('PROFILE')!='parent' && User('PROFILE')!='student')
		echo '<TD width=24><A HREF='.$_SESSION['Search_PHP_SELF'].'&bottom_back=true target=body><IMG SRC=assets/back.gif border=0 vspace=0></A></TD><TD valign=middle class=BottomButton><A HREF='.str_replace('&amp;bottom_back=true','',$_SESSION['Search_PHP_SELF']).'&bottom_back=true target=body>'._('Back to Search Screen').'</A></TD>';     
	
	// PRINT
	echo '<TD width=24><A HREF=Bottom.php?modfunc=print target=body><IMG SRC=assets/print.gif border=0 vspace=0></A></TD><TD valign=middle class=BottomButton><A HREF=Bottom.php?modfunc=print target=body>'._('Print').'</A></TD>';
	// TEMPLATES
	if($_REQUEST['expanded']=='true')
		echo '<TD><A HREF=# onclick="expandFrame();parent.help.location.href=\'Bottom.php?modfunc=templates&expanded=false\';return false;"><IMG SRC=assets/arrow_down.gif border=0 height=5></A></TD>';
	else
		echo '<TD><A HREF=# onclick="expandFrame();parent.help.location.href=\'Bottom.php?modfunc=templates&expanded=true\';return false;"><IMG SRC=assets/arrow_down.gif border=0 height=5></A></TD>';
	// SAVE
	if(User('PROFILE')=='admin')
		echo '<TD width=24><A HREF=Modules.php?modname=Reports/SavedReports.php&modfunc=new target=body><IMG SRC=assets/save_button.gif border=0 vspace=0></A></TD><TD valign=middle class=BottomButton><A HREF=Modules.php?modname=Reports/SavedReports.php&modfunc=new target=body>'._('Save Report').'</A></TD>';
	// HELP
	if($_REQUEST['expanded']=='true')
		echo '<TD><A HREF=# onclick="expandFrame();parent.help.location.href=\'Bottom.php?modname=true&expanded=false\';return false;"><IMG SRC=assets/help.gif border=0 vspace=0></A></TD><TD valign=middle class=BottomButton><A HREF=# onclick="expandFrame();parent.help.location.href=\'Bottom.php?modname=true&expanded=false\';return false;">'._('Help').'</A></TD><TD><A HREF=index.php?modfunc=logout target=_top><IMG SRC=assets/logout.gif border=0 vspace=0 hspace=0></A></TD><TD valign=middle class=BottomButton><A HREF=index.php?modfunc=logout target=_top>'._('Logout').'</A></TD></TR></TABLE>';
	else
		echo '<TD><A HREF=# onclick="expandFrame();parent.help.location.href=\'Bottom.php?modname=true&expanded=true\';return false;"><IMG SRC=assets/help.gif border=0 vspace=0></A></TD><TD valign=middle class=BottomButton><A HREF=# onclick="expandFrame();parent.help.location.href=\'Bottom.php?modname=true&expanded=true\';return false;">'._('Help').'</A></TD><TD><A HREF=index.php?modfunc=logout target=_top><IMG SRC=assets/logout.gif border=0 vspace=0 hspace=0></A></TD><TD valign=middle class=BottomButton><A HREF=index.php?modfunc=logout target=_top>'._('Logout').'</A></TD></TR></TABLE>';
	echo '</CENTER>';
	
	if($_REQUEST['modfunc']=='templates')
	{
		$templates_RET = DBGet(DBQuery("SELECT ID,TITLE FROM HEADER_TEMPLATES ORDER BY TITLE"));
		DrawHeader(_('Document Templates'));
		DrawHeader(_('Choose a template to add to the above report.'));
	
		$content = '<TABLE><TR>';
		foreach($templates_RET as $template)
		{
			$i++;
			if($i%5===0)
				$content .= '</TR><TR>';
			$content .= '<TD>'.DrawTab($template['TITLE'],'Modules.php?_template_id='.$template['ID'].' target=body','CCBBCC','000000','_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF'),'width=100%').'</TD>';
		}
		$content .= '</TR></TABLE><BR><BR><BR>';
		echo DrawHeader($content);
	}
	else
	{
		include 'Help.php';
		include 'Menu.php';
		
		$profile = User('PROFILE');
	
		echo '<DIV class=BottomButton>';
		if($_REQUEST['modname'])
		{
			$_REQUEST['modname'] = $_SESSION['_REQUEST_vars']['modname'];
			echo '<b>'.$_FOCUS['Menu'][$_SESSION['modcat']][$_REQUEST['modname']];
			echo '</b>';
		}
		else
			echo '<b>'._('Welcome to Focus/SIS Help').'</b>';
		
		if($help[$_REQUEST['modname']])
		{
			if($student==true)
				$help[$_REQUEST['modname']] = str_replace('your child','yourself',str_replace('your child\'s','your',$help[$_REQUEST['modname']]));
	
			echo $help[$_REQUEST['modname']];
		}
		elseif($_REQUEST['modname'])
			echo '<P>No help is available for this program.</P>';
		else
			echo $help['default'];
		echo '</DIV>';
	}
	echo '</BODY>';
	echo '</HTML>';
}
?>